<template>
  <div>
    <h3>伤病记录管理</h3>
    <div class="container">
      <vxe-toolbar style="height: 100px;">
        <template #buttons>
          <el-row style="margin-bottom: 20px;" :gutter="20" justify="space-between">
            <el-col :span="4">
              <el-select v-model="searchForm.sportId" size="small" collapse-tags @change="sportChange" filterable
                clearable placeholder="运动项目">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_sport" :key="index">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="searchForm.sportItemId" size="small" collapse-tags @change="sportItemChange"
                filterable clearable placeholder="运动小项">
                <el-option :label="item" :value="key" v-for="(item,key,index) in sportItemData" :key="index">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="searchForm.coachId" size="small" collapse-tags @change="coachChange" filterable
                clearable placeholder="教练员">
                <el-option :label="item" :value="key" v-for="(item,key,index) in coachData" :key="index">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="searchForm.playerId" size="small" collapse-tags filterable clearable
                placeholder="运动员">
                <el-option :label="item.name" :value="item.playerId" v-for="(item,index) in playerData" :key="index">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-button-group style="display: flex;float: right;">
                <el-button type="success" icon="el-icon-download" size="small" @click="exportData">导出</el-button>
                <el-button type="success" icon="el-icon-printer" size="small" @click="printEvent">打印</el-button>
                <el-button type="success" icon="el-icon-plus" size="small" @click="insertEvent">新增</el-button>
              </el-button-group>
            </el-col>
          </el-row>
          <el-row type="flex" :gutter="20" justify="space-between">
            <el-col :span="4">
              <el-select v-model="searchForm.typeId" size="small" collapse-tags filterable clearable placeholder="伤病类型">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_type" :key="index">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="searchForm.partsId" size="small" collapse-tags filterable clearable
                placeholder="伤病部位">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_parts" :key="index">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="searchForm.traumaId" size="small" collapse-tags filterable clearable
                placeholder="创伤类型">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_trauma" :key="index">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="searchForm.oldId" size="small" collapse-tags filterable clearable placeholder="陈旧伤">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_old_wound" :key="index">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4" style="display: flex;">
              <el-button type="success" size="small" icon="el-icon-s-promotion" @click="searchSubmit"
                style="margin: 0 10px;">搜索</el-button>
              <vxe-button @click="searchClear" status="warning" icon="el-icon-refresh-right"> 清空</vxe-button>
            </el-col>
            <el-col :span="4">
              <vxe-input style="float: right;" v-model.trim.lazy="filterName" type="search" placeholder="快捷检索"
                @input="searchEvent"></vxe-input>
            </el-col>
          </el-row>
        </template>
      </vxe-toolbar>
      <vxe-table resizable ref="XTable" show-overflow border max-height="800" stripe highlight-hover-row highlight-current-row
        :loading="loading" :data="dataList" @sort-change="sortChange" :export-config="{}">
        <vxe-table-column field="sport" title="运动项目" sortable></vxe-table-column>
        <vxe-table-column field="sportItem" title="运动小项" sortable></vxe-table-column>
        <vxe-table-column field="coachName" title="教练员" sortable></vxe-table-column>
        <vxe-table-column field="playerName" title="运动员" sortable></vxe-table-column>
        <vxe-table-column field="createDate" title="诊疗时间" sortable></vxe-table-column>
        <vxe-table-column field="name" title="伤病名称" sortable></vxe-table-column>
        <vxe-table-column field="type" title="伤病类型" sortable></vxe-table-column>
        <vxe-table-column field="parts" title="伤病部位" sortable></vxe-table-column>
        <vxe-table-column field="trauma" title="创伤类型" sortable></vxe-table-column>
        <vxe-table-column field="old" title="陈旧伤" sortable></vxe-table-column>
        <vxe-table-column field="vasLevel" title="疼痛等级" sortable></vxe-table-column>
        <vxe-table-column field="prescription" title="处方" sortable></vxe-table-column>
        <vxe-table-column field="diagnosis" title="诊断及治疗意见" sortable></vxe-table-column>
        <vxe-table-column field="step" title="后续跟踪治疗" sortable></vxe-table-column>
        <vxe-table-column field="emp" title="主治医师" sortable></vxe-table-column>
        <vxe-table-column field="reciverDuration" title="恢复用时" sortable></vxe-table-column>
        <vxe-table-column field="memo" title="备注" sortable></vxe-table-column>
        <vxe-table-column field="" title="操作" width="150">
          <template #default="{ row }">
            <el-button-group style="display: flex;">
              <el-tooltip class="item" effect="dark" content="修改" placement="top">
                <el-button @click="editInjuryRecord(row)" type="primary" size="mini" icon="el-icon-edit"></el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="删除" placement="top">
                <el-button @click="deleteInjuryRecord(row)" type="primary" size="mini" icon="el-icon-delete"></el-button>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="复制此条记录为新增模板" placement="top">
                <el-button @click="copyInjuryRecord(row)" type="primary" size="mini" icon="el-icon-tickets"></el-button>
              </el-tooltip>
            </el-button-group>
          </template>
        </vxe-table-column>
      </vxe-table>
      <vxe-pager :loading="loading" :current-page="page.currentPage" :page-size="page.pageSize"
        :total="page.totalResult" :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
        @page-change="handlePageChange">
      </vxe-pager>
      <vxe-modal v-model="insertModal" :title="modelTitle" width="480" height="80%">
        <template #default>
          <el-form ref="insertForm" :rules="rules" :model="insertForm" label-width="150px" inline-message>
            <el-form-item label="运动项目" prop="sportId">
              <el-select :disabled="!insert" v-model="insertForm.sportId" size="small" collapse-tags @change="insertSportChange" filterable
                clearable placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_sport" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="运动小项">
              <el-select :disabled="!insert" v-model="insertForm.sportItemId" size="small" collapse-tags @change="insertSportItemChange"
                filterable clearable placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in sportItemData" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="教练员" prop="coachId">
              <el-select :disabled="!insert" v-model="insertForm.coachId" size="small" collapse-tags @change="insertCoachChange" filterable
                clearable placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in coachData" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="运动员" prop="playerId">
              <el-select :disabled="!insert" v-model="insertForm.playerId" size="small" collapse-tags filterable clearable
                placeholder="请选择">
                <el-option :label="item.name" :value="item.playerId" v-for="(item,index) in playerData" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="诊疗时间" prop="createDate">
              <el-date-picker type="date" placeholder="选择日期" v-model="insertForm.createDate" size="small" style="width: 200px;">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="伤病名称">
              <el-select v-model="insertForm.nameId" size="small" collapse-tags filterable clearable
                placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_name" :key="index">
                </el-option>
              </el-select>
              <el-link v-if="!nameInsertShow" @click="nameInsertShow=true" icon="el-icon-plus" size="small" type="success">点击新增</el-link>
              <el-input v-else style="margin-top: 10px;width: 200px;" size="small" placeholder="请输入伤病名称" v-model="name" class="input-with-select">
                <el-button @click="nameInsert" slot="append" icon="iconfont icon-save"></el-button>
              </el-input>
            </el-form-item>
            <el-form-item label="伤病类型">
              <el-select v-model="insertForm.typeId" size="small" collapse-tags filterable clearable placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_type" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="伤病部位">
              <el-select v-model="insertForm.partsId" size="small" collapse-tags filterable clearable placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_parts" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="创伤类型">
              <el-select v-model="insertForm.traumaId" size="small" collapse-tags filterable clearable
                placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_trauma" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="陈旧伤">
              <el-select v-model="insertForm.oldId" size="small" collapse-tags filterable clearable placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_old_wound" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="疼痛等级">
              <el-select v-model="insertForm.vasLevelId" size="small" collapse-tags filterable clearable
                placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_vas_level" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="处方">
              <el-input v-model="insertForm.prescription" size="small" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="诊断及治疗意见">
              <el-input v-model="insertForm.diagnosis" size="small" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="后续跟踪治疗">
              <el-select v-model="insertForm.stepId" size="small" collapse-tags filterable clearable placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_injury_step" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="主治医师">
              <el-select v-model="insertForm.empId" size="small" collapse-tags filterable clearable placeholder="请选择">
                <el-option :label="item" :value="key" v-for="(item,key,index) in dict_emp" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="恢复用时">
              <el-input v-model="insertForm.reciverDuration" size="small" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="insertForm.memo" size="small" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="warning" size="small" @click="insertModal = false">取消</el-button>
              <el-button type="success" size="small" icon="el-icon-s-promotion" @click="insertSave">保存</el-button>
            </el-form-item>
          </el-form>
        </template>
      </vxe-modal>
    </div>
  </div>
</template>

<script>
  import {
    sport_item_cache,
    pcm4coach,
    meta_sport_item_cache
  } from '@/api/dict.js'
  import {
    injury_record,
    injury_record_dict,
    playersOfThisCoach,
    injury_record_search,
    injury_name_insert,
    injury_name_all,
    injury_record_insert,
    injury_record_update,
    injury_record_delete
  } from '@/api/meta.js'
  export default {
    data() {
      return {
        defaultDataList: [],
        dataList: [],
        page: {
          currentPage: 1,
          pageSize: 10,
          totalResult: 0
        },
        loading: false,
        filterName: '',
        searchProps: [],
        dict_coach: {},
        dict_player: {},
        dict_sport: {},
        dict_emp: {},
        dict_sportItem: {},
        dict_injury_name: {},
        dict_injury_type: {},
        dict_injury_parts: {},
        dict_injury_trauma: {},
        dict_injury_old_wound: {},
        dict_injury_vas_level: {},
        dict_injury_step: {},
        sportItemData: {},
        coachData: {},
        playerData: [],
        searchForm: {
          sportId: '',
          sportItemId: '',
          coachId: '',
          playerId: '',
          typeId: '',
          partsId: '',
          traumaId: '',
          oldId: '',
        },
        insertModal: false,
        insertForm: {
          sportId: '',
          sportItemId: '',
          coachId: '',
          playerId: '',
          createDate: '',
          nameId: '',
          typeId: '',
          partsId: '',
          traumaId: '',
          oldId: '',
          vasLevelId: '',
          prescription: '',
          diagnosis: '',
          reciverDuration: '',
          stepId: '',
          empId: '',
          memo: '',
        },
        rules: {
          sportId: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
          coachId: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
          playerId: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
          createDate: [{
            required: true,
            message: '这是必填项',
            trigger: 'change'
          }, ],
        },
        modelTitle: '新增记录',
        nameInsertShow: false,
        name: '',
        insert: true,
        recordId: '',
      }
    },
    created() {
      this.init_dict().then(() => {
        this.injury_record_all()
      })
    },
    methods: {
      copyInjuryRecord(row) {
        this.insertEvent()
        this.sportItemData[row.sportItemId] = row.sportItem
        this.coachData[row.coachId] = row.coachName
        this.playerData = [{playerId:row.playerId,name:row.playerName}]
        this.insertForm.sportId = row.sportId+''
        this.insertForm.sportItemId = row.sportItemId?row.sportItemId+'':''
        this.sportChange(row.sportId)
        this.insertForm.coachId = row.coachId+''
        this.coachChange(row.coachId)
        this.insertForm.createDate = row.createDate
        this.insertForm.nameId = row.nameId?row.nameId+'':''
        this.insertForm.typeId = row.typeId?row.typeId+'':''
        this.insertForm.partsId = row.partsId?row.partsId+'':''
        this.insertForm.traumaId = row.traumaId?row.traumaId+'':''
        this.insertForm.oldId = row.oldId?row.oldId+'':''
        this.insertForm.vasLevelId = row.vasLevelId?row.vasLevelId+'':''
        this.insertForm.prescription = row.prescription
        this.insertForm.diagnosis = row.diagnosis
        this.insertForm.reciverDuration = row.reciverDuration
        this.insertForm.stepId = row.stepId?row.stepId+'':''
        this.insertForm.empId = row.empId?row.empId+'':''
        this.insertForm.memo = row.memo
        this.recordId = row.recordId
      },
      insertSave() {
        this.$refs.insertForm.validate((valid) => {
          if(valid){
            let data = {}
            if(this.insertForm.sportId) data.sportId = this.insertForm.sportId
            if(this.insertForm.sportItemId) data.sportItemId = this.insertForm.sportItemId
            if(this.insertForm.coachId) data.coachId = this.insertForm.coachId
            if(this.insertForm.playerId) data.playerId = this.insertForm.playerId
            if(this.insertForm.createDate) data.createDate = this.$moment(this.insertForm.createDate).format('YYYY-MM-DD') + ' 00:00:00'
            if(this.insertForm.nameId) data.nameId = this.insertForm.nameId
            if(this.insertForm.typeId) data.typeId = this.insertForm.typeId
            if(this.insertForm.partsId) data.partsId = this.insertForm.partsId
            if(this.insertForm.traumaId) data.traumaId = this.insertForm.traumaId
            if(this.insertForm.oldId) data.oldId = this.insertForm.oldId
            if(this.insertForm.vasLevelId) data.vasLevelId = this.insertForm.vasLevelId
            if(this.insertForm.prescription) data.prescription = this.insertForm.prescription
            if(this.insertForm.diagnosis) data.diagnosis = this.insertForm.diagnosis
            if(this.insertForm.reciverDuration) data.reciverDuration = this.insertForm.reciverDuration
            if(this.insertForm.stepId) data.stepId = this.insertForm.stepId
            if(this.insertForm.empId) data.empId = this.insertForm.empId
            if(this.insertForm.memo) data.memo = this.insertForm.memo
            if(this.insert){
              injury_record_insert(data).then(res=>{
                if(res.code === 200) {
                  this.$message.success('数据保存成功！')
                  this.injury_record_all()
                  this.insertModal = false
                }else{
                  this.$message.error(res.message)
                }
              })
            }else{
              data.recordId = this.recordId
              injury_record_update(data).then(res=>{
                if(res.code === 200) {
                  this.$message.success('数据保存成功！')
                  this.injury_record_all()
                  this.insertModal = false
                }else{
                  this.$message.error(res.message)
                }
              })
            }
          }else{
            return false
          }
        })
      },
      nameInsert() {
        injury_name_insert({name:this.name}).then(res=>{
          if(res.code === 200) {
            this.$message.success('添加成功')
            injury_name_all().then(res => {
              this.dict_injury_name = this.injury_record_dict(res.data,'nameId')
            })
          }else{
            this.$message.error(res.message)
          }
          this.nameInsertShow = false
          this.name = ''
        })
      },
      insertSportChange(val) {
        this.insertForm.sportItemId = ''
        this.insertForm.coachId = ''
        this.insertForm.playerId = ''
        this.sportItemData = {}
        this.coachData = {}
        this.playerData = []
        let sportId = this.insertForm.sportId || val
        if (sportId) {
          meta_sport_item_cache({
            sportId: sportId
          }).then(res => {
            this.sportItemData = res
          })
          pcm4coach({
            sportId: sportId
          }).then(res => {
            this.coachData = res
          })
        }
      },
      insertSportItemChange(val) {
        this.insertForm.coachId = ''
        this.insertForm.playerId = ''
        this.coachData = {}
        this.playerData = []
        let sportItemId = this.insertForm.sportItemId || val
        let data = {}
        if (this.insertForm.sportId) data.sportId = this.insertForm.sportId
        if (this.insertForm.sportItemId) data.sportItemId = this.insertForm.sportItemId
        if (sportItemId) {
          pcm4coach(data).then(res => {
            this.coachData = res
          })
        }
      },
      insertCoachChange(val) {
        let data = {
          coachId: this.insertForm.coachId,
          isImportant: 3
        }
        this.insertForm.playerId = ''
        playersOfThisCoach(data).then(res => {
          this.playerData = res.data
        })
      },
      insertEvent() {
        this.insertForm.playerId = ''
        this.insert = true
        this.modelTitle = '新增记录'
        this.insertModal = true
        this.$nextTick(()=>{
          this.$refs.insertForm.clearValidate()
        })
      },
      exportData() {
        this.$refs.XTable.exportData({
          filename: this.$route.meta.title, type: 'csv',
          data: this.search?this.dataList:this.defaultDataList
        })
      },
      printEvent() {
        let topHtml = '<h2>伤病记录</h2>'
        this.searchEvent()
        setTimeout(() => {
          this.$refs.XTable.print({
            sheetName: '伤病记录',
            beforePrintMethod: ({
              content
            }) => {
              return topHtml + content
            }
          })
        }, 1000)
      },
      searchSubmit() {
        this.loading = true
        let data = {}
        if (this.searchForm.sportId) data.sportId = this.searchForm.sportId
        if (this.searchForm.sportItemId) data.sportItemId = this.searchForm.sportItemId
        if (this.searchForm.coachId) data.coachId = this.searchForm.coachId
        if (this.searchForm.playerId) data.playerId = this.searchForm.playerId
        if (this.searchForm.typeId) data.typeId = this.searchForm.typeId
        if (this.searchForm.partsId) data.partsId = this.searchForm.partsId
        if (this.searchForm.traumaId) data.traumaId = this.searchForm.traumaId
        if (this.searchForm.oldId) data.oldId = this.searchForm.oldId
        injury_record_search(data).then(res => {
          if (res.code === 200 && res.data.length!=0) {
            this.init_data(res.data)
          } else {
            this.defaultDataList = []
          }
        }).then(() => {
          this.findList()
        })
      },
      searchClear() {
        this.searchForm = {
          sportId: '',
          sportItemId: '',
          coachId: '',
          playerId: '',
          typeId: '',
          partsId: '',
          traumaId: '',
          oldId: '',
        }
        this.searchSubmit()
      },
      sportChange(val) {
        this.searchForm.sportItemId = ''
        this.searchForm.coachId = ''
        this.searchForm.playerId = ''
        this.sportItemData = {}
        this.coachData = {}
        this.playerData = []
        let sportId = this.searchForm.sportId || val
        if (sportId) {
          meta_sport_item_cache({
            sportId: sportId
          }).then(res => {
            this.sportItemData = res
          })
          pcm4coach({
            sportId: sportId
          }).then(res => {
            this.coachData = res
          })
        }
      },
      sportItemChange(val) {
        this.searchForm.coachId = ''
        this.searchForm.playerId = ''
        this.coachData = {}
        this.playerData = []
        let sportItemId = this.searchForm.sportItemId || val
        let data = {}
        if (this.searchForm.sportId) data.sportId = this.searchForm.sportId
        if (this.searchForm.sportItemId) data.sportItemId = this.searchForm.sportItemId
        if (sportItemId) {
          pcm4coach(data).then(res => {
            this.coachData = res
          })
        }
      },
      coachChange(val) {
        let data = {
          coachId: this.searchForm.coachId || val,
          isImportant: 3
        }
        playersOfThisCoach(data).then(res => {
          this.playerData = res.data
        })
      },
      editInjuryRecord(row) {
        this.sportItemData[row.sportItemId] = row.sportItem
        this.coachData[row.coachId] = row.coachName
        this.playerData = [{playerId:row.playerId,name:row.playerName}]
        this.insertForm.sportId = row.sportId+''
        this.insertForm.sportItemId = row.sportItemId?row.sportItemId+'':''
        this.insertForm.coachId = row.coachId+''
        this.insertForm.playerId = row.playerId
        this.insertForm.createDate = row.createDate
        this.insertForm.nameId = row.nameId?row.nameId+'':''
        this.insertForm.typeId = row.typeId?row.typeId+'':''
        this.insertForm.partsId = row.partsId?row.partsId+'':''
        this.insertForm.traumaId = row.traumaId?row.traumaId+'':''
        this.insertForm.oldId = row.oldId?row.oldId+'':''
        this.insertForm.vasLevelId = row.vasLevelId?row.vasLevelId+'':''
        this.insertForm.prescription = row.prescription
        this.insertForm.diagnosis = row.diagnosis
        this.insertForm.reciverDuration = row.reciverDuration
        this.insertForm.stepId = row.stepId?row.stepId+'':''
        this.insertForm.empId = row.empId?row.empId+'':''
        this.insertForm.memo = row.memo
        this.recordId = row.recordId
        this.modelTitle = '修改记录'
        this.insert = false
        this.insertModal = true
      },
      deleteInjuryRecord(row) {
        this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          injury_record_delete({recordId:row.recordId}).then(res => {
            if (res.code === 200) {
              this.injury_record_all()
              this.$message.success('删除成功！')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      injury_record_all() {
        this.loading = true
        injury_record().then(res => {
          if (res.code === 200) {
            this.init_data(res.data)
          } else {
            this.$message.error(res.message)
            this.defaultDataList = []
          }
        }).then(() => {
          this.findList()
        })
      },
      init_data(data) {
        if(data.length==0) return
        if (data.length != 0) {
          data.forEach(item => {
            item.sport = this.dict_sport[item.sportId]
            item.sportItem = this.dict_sportItem[item.sportItemId]
            item.emp = this.dict_emp[item.empId]
            item.name = this.dict_injury_name[item.nameId]
            item.type = this.dict_injury_type[item.typeId]
            item.parts = this.dict_injury_parts[item.partsId]
            item.trauma = this.dict_injury_trauma[item.traumaId]
            item.old = this.dict_injury_old_wound[item.oldId]
            item.vasLevel = this.dict_injury_vas_level[item.vasLevelId]
            item.step = this.dict_injury_step[item.stepId]
            item.createDate = this.$moment(item.createDate).format('YYYY-MM-DD')
          })
          this.searchProps = Object.keys(data[0])
          this.defaultDataList = data.reverse()
        } else {
          this.defaultDataList = []
        }
      },
      init_dict() {
        return new Promise((resolve, reject) => {
          sport_item_cache().then(res => {
            this.dict_sportItem = res
          })
          injury_record_dict().then(res => {
            if (res.code === 200) {
              this.dict_emp = this.injury_record_dict(res.data.employeeList, 'empId')
              this.dict_sport = this.injury_record_dict(res.data.sportList, 'sportId')
              this.dict_injury_name = this.injury_record_dict(res.data.injuryNameList, 'nameId')
              this.dict_injury_type = this.injury_record_dict(res.data.injuryTypeList, 'typeId')
              this.dict_injury_parts = this.injury_record_dict(res.data.injuryPartsList, 'partsId')
              this.dict_injury_trauma = this.injury_record_dict(res.data.injuryTraumaList, 'traumaId')
              this.dict_injury_old_wound = this.injury_record_dict(res.data.injuryOldWoundList, 'oldId')
              this.dict_injury_vas_level = this.injury_record_dict(res.data.injuryVasLevelList, 'vasLevelId')
              this.dict_injury_step = this.injury_record_dict(res.data.injuryStepList, 'stepId')
            } else {
              this.$message.error(res.message)
            }
            resolve()
          })
        })
      },
      injury_record_dict(arr, id) {
        let dict = {}
        arr.forEach(item => {
          dict[item[id]] = item.name
        })
        return dict
      },
      searchEvent() {
        const filterName = this.$XEUtils.toValueString(this.filterName).trim().toLowerCase()
        if (filterName) {
          const filterRE = filterName
          const searchProps = this.searchProps
          const rest = this.defaultDataList.filter(item => searchProps.some(key => this.$XEUtils.toValueString(item[
            key]).toLowerCase().indexOf(filterName) > -1))
          this.dataList = rest.map(row => {
            const item = Object.assign({}, row)
            searchProps.forEach(key => {
              item[key] = this.$XEUtils.toValueString(item[key]).replace(filterRE, match => match)
            })
            return item
          })
        } else {
          this.dataList = this.defaultDataList
     }
      },
      handlePageChange({
        currentPage,
        pageSize
      }) {
        this.page.currentPage = currentPage
        this.page.pageSize = pageSize
        this.findList()
      },
      sortChange() {
        this.page.currentPage = 1
        this.page.pageSize = this.defaultDataList.length
        this.findList()
      },
      findList() {
        setTimeout(() => {
          this.loading = false
          this.page.totalResult = this.defaultDataList.length
          this.dataList = this.defaultDataList.slice((this.page.currentPage - 1) * this.page
            .pageSize, this.page.currentPage * this.page.pageSize)
        }, 300)
      },
    }
  }
</script>

<style lang="scss" scoped>
  .container {
    padding: 10px;
    background-color: #FFFFFF;
  }
</style>
